{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![QuantConnect Logo](https://cdn.quantconnect.com/web/i/qc_notebook_logo_rev0.png)\n",
    "## Welcome to The QuantConnect Research Page\n",
    "#### Refer to this page for documentation https://www.quantconnect.com/docs/research/overview#\n",
    "#### Contribute to this template file https://github.com/QuantConnect/Lean/blob/master/Research/KitchenSinkCSharpQuantBookTemplate.ipynb"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## QuantBook Basics\n",
    "The following example is ready to be used in our Docker container, reference the readme for more details on setting this up.\n",
    "\n",
    "In order to use this notebook locally you will need to make a few small changes:\n",
    "\n",
    "1. Either create the notebook in your build folder (`bin/debug`) **or** set working directory of the notebook to it like so in the first cell:\n",
    "\n",
    "    ```\n",
    "    using System.IO\n",
    "    Directory.SetCurrentDirectory(\"PathToLean/Lean/Launcher/bin/Debug/\");\n",
    "    ```\n",
    "\n",
    "\n",
    "2. Load \"QuantConnect.csx\" instead of \"../QuantConnect.csx\", this is again because of the Notebook position relative to the build files. \n",
    "\n",
    "### Start QuantBook\n",
    "- Load \"Initialize.csx\" to load our assemblies into our C# Kernel\n",
    "- Load \"QuantConnect.csx\" with all the basic imports\n",
    "- Create a QuantBook instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// We need to load assemblies at the start in their own cell\n",
    "#load \"../Initialize.csx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Load in our startup script, this creates our Api Object\n",
    "#load \"../QuantConnect.csx\"\n",
    "\n",
    "// Load in the namespaces we are going to use\n",
    "using QuantConnect;\n",
    "using QuantConnect.Data;\n",
    "using QuantConnect.Data.Custom;\n",
    "using QuantConnect.Data.Market;\n",
    "using QuantConnect.Research;\n",
    "using QuantConnect.Algorithm;\n",
    "\n",
    "var qb = new QuantBook();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Using the Web API\n",
    "Our script `QuantConnect.csx` automatically loads an instance of the web API for you to use.**\n",
    "\n",
    "Look at Lean's [Api](https://github.com/QuantConnect/Lean/tree/master/Api) class for more functions to interact with the cloud\n",
    "\n",
    "\n",
    "##### **Note: This will only connect if you have your User ID and Api token in `config.json` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Show that our api object is connected to the Web Api\n",
    "Console.WriteLine(api.Connected);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Get our list of projects from the cloud and print their names\n",
    "var projectResponse = api.ListProjects();\n",
    "foreach (var project in projectResponse.Projects) {\n",
    "    Console.WriteLine(project.Name);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Selecting Asset Data\n",
    "Checkout the QuantConnect [docs](https://www.quantconnect.com/docs#Initializing-Algorithms-Selecting-Asset-Data) to learn how to select asset data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "var spy = qb.AddEquity(\"SPY\");\n",
    "var eur = qb.AddForex(\"EURUSD\");\n",
    "var btc = qb.AddCrypto(\"BTCUSD\");\n",
    "var fxv = qb.AddData<FxcmVolume>(\"EURUSD_Vol\", Resolution.Hour);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Historical Data Requests\n",
    "\n",
    "We can use the QuantConnect API to make Historical Data Requests. The data will be presented as multi-index pandas.DataFrame where the first index is the Symbol.\n",
    "\n",
    "For more information, please follow the [link](https://www.quantconnect.com/docs#Historical-Data-Historical-Data-Requests)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution\n",
    "var h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Gets historical data from the subscribed assets, from the last 30 days with daily resolution\n",
    "var h2 = qb.History(qb.Securities.Keys, TimeSpan.FromDays(360), Resolution.Daily);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Gets historical data from the subscribed assets, between two dates with daily resolution\n",
    "var h3 = qb.History(btc.Symbol, new DateTime(2014,1,1), DateTime.Now, Resolution.Daily);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Only fetchs historical data from a desired symbol\n",
    "var h4 = qb.History(spy.Symbol, 360, Resolution.Daily);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Only fetchs historical data from a desired symbol\n",
    "var h5 = qb.History<QuoteBar>(eur.Symbol, TimeSpan.FromDays(360), Resolution.Daily);"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "// Fetchs custom data\n",
    "var h6 = qb.History<FxcmVolume>(fxv.Symbol, TimeSpan.FromDays(360));"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "language_info": {
   "file_extension": ".cs",
   "mimetype": "text/x-csharp",
   "name": "C#",
   "pygments_lexer": "csharp",
   "version": "9.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}